/**
 * jims
 */
package com.jims.master.modules.drug.service;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.modules.drug.dao.DrugInfoDao;
import com.jims.master.modules.drug.dao.DrugPriceDao;
import com.jims.master.modules.drug.entity.DrugInfo;
import com.jims.master.modules.drug.entity.DrugPrice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 药品基本信息Service
 *
 * @author CTQ
 * @version 2017-03-02
 */
@Service
@Transactional(readOnly = true)
public class DrugInfoService extends CrudService<DrugInfoDao, DrugInfo> {

    @Autowired
    private DrugPriceDao drugPriceDao;

    public DrugInfo get(String id) {
        return super.get(id);
    }

    /**
     * 添加价表数据自动补全过滤已添加过的药品
     *
     * @param drugInfo
     * @param orgId
     * @return
     */
    public List<DrugInfo> findAutoCompleteList(DrugInfo drugInfo, String orgId, String q, String limit) {
        return dao.findAutoCompleteList(drugInfo, orgId, q, limit);
    }

    /**
     * 药品入库自动补全已存在价格的药品
     *只选择大包装
     * @param drugInfo
     * @param orgId
     * @return
     */
    public List<DrugInfo> findAutoCompletePriceist(DrugInfo drugInfo, String orgId, String q, String limit) {
        drugInfo.updateOrgId();
        return dao.findAutoCompletePriceList(drugInfo,q,limit);
    }

//    public List<DrugInfo> findAutoCompletePriceistNew(DrugInfo drugInfo, String orgId, String q, String limit) {
//        drugInfo.updateOrgId();
//        return dao.findAutoCompletePriceListNew(drugInfo, q, limit);
//    }
    public Page<DrugInfo> findPage(Page<DrugInfo> page, DrugInfo drugInfo) {
        return super.findPage(page, drugInfo);
    }

    @Transactional(readOnly = false)
    public int save(DrugInfo drugInfo) {
        return super.save(drugInfo);
    }

    @Transactional(readOnly = false)
    public int delInfo(DrugInfo drugInfo) {
        int num = 0;
        DrugPrice dp = new DrugPrice();
        dp.setDrugInfo(drugInfo);
        List<DrugPrice> list = drugPriceDao.findByDrugInfo(dp);
        if (list != null && list.size() > 0) {
            num = -1;
        } else {
            num = dao.delete(drugInfo);
        }
        return num;
    }

    /**
     * 用药品类别、药品名称查询药品基本信息
     *
     * @param drugInfo
     * @return
     */
    public Page<DrugInfo> findDrugInfoList(Page<DrugInfo> page, DrugInfo drugInfo) {
        drugInfo.updateOrgId();
        drugInfo.setPage(page);
        page.setList(dao.findDrugInfoList(drugInfo));
        return page;
    }

    public int findByDrugId(DrugInfo drugInfo){
        int i=0;
        i=dao.findByDrugId(drugInfo);
        return i;
    }

}